/**
 * Copyright (C) 2010 Neofonie GmbH
 *
 * This programm is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicode.analysis.demonstrator.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/** Utils for scanning directories for content.*/
public class FileSystemScanner {
  /**Logger for this class*/
  private static final Logger LOG = LoggerFactory.getLogger(FileSystemScanner.class);

  /** Get the collocation file for the given query string.
   * @param queryString the query to generate collocations for.
   * @return the File containing the collocation results for the given query.
   */
  public File getFile(final String queryString, final String basePath) throws FileNotFoundException {
    LOG.info("Using base path: " + basePath + " and file: " + queryString);
    StringBuilder builder = new StringBuilder(basePath).append("/").append(queryString);
    File queryFile = new File(builder.toString());
    String[] timestamps = queryFile.list();

    String subdir = "";
    String prevdir = "";
    for (String timestamp : timestamps) {
      if (subdir.compareTo(timestamp) < 0 || subdir.isEmpty()) {
        prevdir = subdir;
        subdir = timestamp;
      }
    }

    builder.append("/");
    File subdirContent = new File(builder.toString() + subdir);
    String [] collocations = subdirContent.list();
    if (collocations.length < 1) {
      File prevdirContent = new File (builder.toString() + prevdir);
      collocations = prevdirContent.list();
      if (collocations.length < 1) {
        LOG.warn("Invalid query found for collocation display function: " + queryString);
        throw new FileNotFoundException("File for collocations not found for query: " + queryString);
      }
      builder.append(prevdir).append("/").append(collocations[0]);
    } else {
      builder.append(subdir).append("/").append(collocations[0]);
    }
    return new File(builder.toString());
  }

  public Collection<String> getQueries(final String basePath) {
    LOG.info("Using base path: " + basePath);
    Set<String> queries = new HashSet<String>();
    File baseFile = new File(basePath);
    System.out.println(baseFile);
    String[] queryArray = baseFile.list();
    queries.addAll(Arrays.asList(queryArray));
    return queries;
  }
}
